package com.shop.admin.api;

import com.shop.admin.aspect.interce.PowerCheckInterce;
import com.shop.admin.model.po.dto.DiscountPreDto;
import com.shop.admin.service.DiscountService;
import com.shop.po.Discount;
import com.shop.po.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author banxia
 * @since 2023-02-26
 */
@RestController
@RequestMapping("/discount")
public class DiscountController {


    @Autowired
    private DiscountService discountService;


    //提供Feign接口调用，存在安全问题--->未解决！！！
    //尝试修改优惠券张数
    @PostMapping("/changDiscountCount/{discountId}/{count}")
    public boolean changDiscountCount(@PathVariable("discountId") Long discountId,
                                      @PathVariable("count") Long count){
        boolean b = discountService.changDiscountCount(discountId, count);
        return b;
    }

    /**
     * 添加优惠券，这里并不会直接把优惠券发送到discount表中
     * 这里会把它发到待发送表中，之后再通过分布式任务调度系统调用
     * 未测试。。。出bug了，导致@RequestBody注解失效
     * @param discountPreDto
     */
    @PostMapping("/addDiscount")
    @PowerCheckInterce(needPower = "cr")
    public R addDiscount(@RequestBody DiscountPreDto discountPreDto){
        discountService.addDiscount(discountPreDto);
        return R.ok("成功添加优惠券");
    }


    /**
     * 提供Feign调用
     * 通过优惠券ID集合获取到信息
     * @param discountIds
     * @return
     */
    @GetMapping("/getDiscountBatch")
    public List<Discount> getDiscountBatch(@RequestParam("discountIds") List<Long> discountIds){
        List<Discount> discountBatch = discountService.getDiscountBatch(discountIds);
        return discountBatch;
    }

    /**
     * 通过优惠券ID获取到信息（提高Feign供调用）
     * @param discountId
     * @return
     */
    @GetMapping("/getDiscountById/{discountId}")
    public Discount getDiscountById(@PathVariable("discountId") Long discountId){
        Discount discount = discountService.getDiscountById(discountId);
        return discount;
    }


}

